package com.example.weather_app.diary;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DiaryDBHelper extends SQLiteOpenHelper {
    private static final String TAG = "DiaryDBHelper";
    private static final String DATABASE_NAME = "diary.db";
    private static final int DATABASE_VERSION = 3;  // 升级到版本3

    public static final String TABLE_NAME = "diaries";
    public static final String COL_ID = "id";
    public static final String COL_TITLE = "title";
    public static final String COL_CONTENT = "content";
    public static final String COL_DATE = "date";
    public static final String COL_REMINDER_TIME = "reminder_time";  // 提醒时间列
    public static final String COL_CREATED_AT = "created_at";

    public DiaryDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableQuery = "CREATE TABLE " + TABLE_NAME + " (" +
                COL_ID + " TEXT PRIMARY KEY," +
                COL_TITLE + " TEXT," +
                COL_CONTENT + " TEXT," +
                COL_DATE + " TEXT," +
                COL_REMINDER_TIME + " TEXT," +  // 新增提醒时间列
                COL_CREATED_AT + " TEXT DEFAULT CURRENT_TIMESTAMP" +
                ")";
        db.execSQL(createTableQuery);
        Log.d(TAG, "Diary table created.");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            // 从版本1升级到2：添加reminder_time列
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COL_REMINDER_TIME + " TEXT");
            Log.d(TAG, "Added reminder_time column to diary table.");
        }

        if (oldVersion < 3) {
            // 从版本2升级到3：添加空的升级逻辑（保持功能不变）
            Log.d(TAG, "Database upgraded to version 3 (no structural changes).");
        }
    }
}